package mamma.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import mamma.common.FileService;
import mamma.common.PageNavigator;
import mamma.dao.PhotoDao;
import mamma.vo.MemberVo;
import mamma.vo.PhotoCommentVo;
import mamma.vo.PhotoVo;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.json.simple.JSONValue;

import com.opensymphony.xwork2.ActionSupport;

public class PhotoAction extends ActionSupport implements SessionAware {
	private int id;
	private int currentPage = 1;
	private int page;
	private int startRecord;
	private int likeit;
	private int lastPage;
	
	private File upload;
	
	private String uploadFileName;
	private String uploadContentType;
	private String loginId;
	private String savedFile;
	private String comment;
	private String nickname;
	private String email;
	
	private PhotoCommentVo[] photoCommentList;
	
	private PhotoVo photo;
	private MemberVo memberVo;
	private PageNavigator navi;
	
	private HttpServletRequest request;
	private Map<String, Object> session;
	private List<PhotoVo> photoList;


	/*사진을 넣고 프로필 바꾸기*/
	public String insertProfile() throws Exception{
		//프로필로 등록한 사진 서버에 업로드
		photo = new PhotoVo();
		FileService fs = new FileService();
		String basePath = ServletActionContext.getServletContext().getRealPath("/photoData");

		String savedfile = fs.saveFile(upload, basePath, uploadFileName);
		photo.setSavedFile(savedfile);
		photo.setOriginalFile(uploadFileName);


		// session 에서 회원정보 가져와서 방금 업로드한 프로필 사진 setting 하기
		MemberVo loginVo =(MemberVo)session.get("login");

		String email = loginVo.getEmail();
		String nickname = loginVo.getNickname();
		photo.setEmail(email);
		photo.setNickname(nickname);

		PhotoDao dao = new PhotoDao();
		dao.insertProfile(photo);
		int profile_id = dao.selectId(photo.getSavedFile());
		System.out.println("profile_id"+profile_id);
		dao.memberUpdate(email, profile_id);
		
		gtProfileInfo();
		
		return SUCCESS;
	}

	/*사진 넣기*/
	public String insertPhoto() throws Exception{
		System.out.println("insertPhoto method activated");
		FileService fs = new FileService();
		
		/* memberVo에 정보 넣기 */
		memberVo = (MemberVo)session.get("login");

		PhotoDao dao= new PhotoDao();


		String basePath = ServletActionContext.getServletContext().getRealPath("/photoData");
		String savedfile = fs.saveFile(upload, basePath, uploadFileName);
		/* photo update에 필요한 정보 넣기*/
		photo.setSavedFile(savedfile);
		photo.setOriginalFile(uploadFileName);
		photo.setEmail(memberVo.getEmail());
		photo.setNickname(memberVo.getNickname());
		dao.insertPhoto(photo);
		
		System.out.println("insertPhoto method end");
		return SUCCESS;
	}
	/*session의 email로 member 객체를 가져 옴*/
	public String gtProfileInfo(){
		memberVo = new MemberVo();
		MemberVo temp = (MemberVo)session.get("login");
		loginId = temp.getEmail();
		PhotoDao dao= new PhotoDao();
		memberVo = dao.getMember(loginId);
		savedFile = dao.gtProfile(memberVo.getProfile_id());
		
		return SUCCESS;
	}
	/* 별명 비밀번호 거주지역 변 to the 경 */
	public String profileUpdate(){

		PhotoDao dao = new PhotoDao();
		dao.updateProfile(memberVo);

		return SUCCESS;
	}
	/* 사진 객체 찾아 savedFile 와 ID 값 가져오기*/
	public String photoList(){
		memberVo = new MemberVo();
		if (session.get("login")==null) {}
		
		PhotoDao dao = new PhotoDao();
		int countPerPage = 20;
		int pagePerGroup = 5;
		int totalRecordsCount = dao.totalPhoto(email);
		PageNavigator navi = new PageNavigator(countPerPage, pagePerGroup, page, totalRecordsCount);
		System.out.println(navi.getStartRecord());
		System.out.println(navi.getStartPageGroup());
		System.out.println(page);
		photoList = dao.photoList(email, navi.getStartRecord(), navi.getCountPerPage());
		
		lastPage = navi.getTotalPageCount();
		
		return SUCCESS;
	}
	
	public String selectPhoto(){
		PhotoDao dao = new PhotoDao();
		PhotoVo vo = new PhotoVo();
		vo.setId(id); vo.setEmail(email);
		System.out.println(vo);
		photo = dao.getPhoto(vo);
		System.out.println(photo);
		return SUCCESS;
	}
	
	public String selectPhotoComment(){
		System.out.println("selectPhotoComment method activated");
		PhotoDao dao = new PhotoDao();
		
		MemberVo temp = (MemberVo)session.get("login");
		String email = temp.getEmail();
		
		int countPerPage = 10;
		
		PhotoCommentVo vo = new PhotoCommentVo();
		vo.setEmail(email);	vo.setPage(page); vo.setCount(countPerPage); vo.setId(id);
		
		List<PhotoCommentVo> list = dao.selectPhotoComment(vo);
		photoCommentList = list.toArray(new PhotoCommentVo[list.size()]);
		
		return SUCCESS;
	}
	
	public String insertPhotoComment(){
		System.out.println("insertPhotoComment method activated");
		
		MemberVo loginVo =(MemberVo)session.get("login");

		String email = loginVo.getEmail();
		String nickname = loginVo.getNickname();
		
		PhotoCommentVo vo = new PhotoCommentVo();
		vo.setContent(comment); vo.setId(id);
		vo.setEmail(email); vo.setNickname(nickname);
		
		PhotoDao dao = new PhotoDao();
		dao.insertPhotoComment(vo);
		
		return SUCCESS;
	}
	
	public String likeit(){
		System.out.println("likeit method activated");
		
		PhotoDao dao = new PhotoDao();
		likeit = dao.likeit(id);
		
		return SUCCESS;
	}
	
	public String deletePhoto(){
		System.out.println("deletePhoto method activated");
		
		PhotoDao dao = new PhotoDao();
		dao.deletePhoto(id);
		
		return SUCCESS;
	}
	
	
	// ========= setter & getter ========
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public PhotoVo getPhoto() {
		return photo;
	}

	public void setPhoto(PhotoVo photo) {
		this.photo = photo;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public MemberVo getMemberVo() {
		return memberVo;
	}

	public void setMemberVo(MemberVo memberVo) {
		this.memberVo = memberVo;
	}

	public String getSavedFile() {
		return savedFile;
	}

	public void setSavedFile(String savedFile) {
		this.savedFile = savedFile;
	}

	public List<PhotoVo> getPhotoList() {
		return photoList;
	}

	public void setPhotoList(List<PhotoVo> photoList) {
		this.photoList = photoList;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public PageNavigator getNavi() {
		return navi;
	}

	public void setNavi(PageNavigator navi) {
		this.navi = navi;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public PhotoCommentVo[] getPhotoCommentList() {
		return photoCommentList;
	}

	public void setPhotoCommentList(PhotoCommentVo[] photoCommentList) {
		this.photoCommentList = photoCommentList;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getStartRecord() {
		return startRecord;
	}

	public void setStartRecord(int startRecord) {
		this.startRecord = startRecord;
	}

	public int getLikeit() {
		return likeit;
	}

	public void setLikeit(int likeit) {
		this.likeit = likeit;
	}

	public int getLastPage() {
		return lastPage;
	}

	public void setLastPage(int lastPage) {
		this.lastPage = lastPage;
	}
	
}
